package com.vxdata.utils.modules.word.poi_tl;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.vxdata.common.utils.StringUtils;
import com.vxdata.utils.modules.word.poi_tl.vo.CulturalActivity;
import com.vxdata.utils.modules.word.poi_tl.vo.CulturalMaterial;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PoiTlWordUtil {
    /**
     * 模板路径
     */
    @Value("${word.templateUrl}")
    private String templateUrl;

    /**
     * 实体生成Word
     *
     * @param culturalActivity 活动信息
     */
    public void beanToWord(HttpServletResponse response, CulturalActivity culturalActivity) {
        try {
            Map<String, Object> map = getData(culturalActivity);
            generateWord(response, templateUrl, map);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 实体生成Word, 输出到本地路径中
     *
     * @param culturalActivity
     * @param fileUrl
     */
    public void beanToWordLocal(CulturalActivity culturalActivity, String fileUrl) {
        try {
            Map<String, Object> map = getData(culturalActivity);
            generateWord(templateUrl, fileUrl, map);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 替换模板中的字段
     * 输出到本地路径中
     *
     * @param templateUrl 模板地址
     * @param outUrl      输出word地址
     * @param map         替换信息
     * @throws IOException
     */
    public static void generateWord(String templateUrl, String outUrl, Map<String, Object> map) throws IOException {
        XWPFTemplate render = XWPFTemplate.compile(templateUrl).render(map);
        render.writeToFile(outUrl);
    }

    /**
     * 替换模板中的字段
     * 直接响应前端
     *
     * @param response    前端响应
     * @param templateUrl 模板路径
     * @param map         替换信息
     * @throws IOException
     */
    public static void generateWord(HttpServletResponse response, String templateUrl, Map<String, Object> map) throws IOException {
        XWPFTemplate render = XWPFTemplate.compile(templateUrl).render(map);
        render.write(response.getOutputStream());
    }

    /**
     * 根据活动信息构建word需要的map
     *
     * @param activity 活动信息
     * @return
     */
    public static Map<String, Object> getData(CulturalActivity activity) {
        Map<String, Object> map = new HashMap<>();
        // 时间
        DateTimeFormatter dfDate = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        map.put("date", dfDate.format(activity.getHoldTime()));
        // 地点
        map.put("place", activity.getActivityAddr() == null ? "" : activity.getActivityAddr());
        // 活动类别
        map.put("type", activity.getActivityType() == null ? "" : activity.getActivityType());
        // 人次
        map.put("number", activity.getPeopleNumber() == null ? "" : activity.getPeopleNumber().toString());
        // 主题
        map.put("name", activity.getActivityName() == null ? "" : activity.getActivityName());
        // 活动记录
        map.put("record", activity.getActivityRecord() == null ? "" : activity.getActivityRecord());

        // 材料清单, 先规定全部是 □
        map.put("0", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("1", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("2", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("3", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("4", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("5", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("6", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("7", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        map.put("8", new TextRenderData("\u00A3", new Style("Wingdings 2", 14)));
        List<CulturalMaterial> materialList = activity.getCulturalMaterialList();
        if (materialList != null && materialList.size() > 0) {
            for (CulturalMaterial material : materialList) {
                // 如果有这个类型的材料就 改成 对钩
                if (StringUtils.isNotEmpty(material.getType())) {
                    map.put(material.getType(), new TextRenderData("R", new Style("Wingdings 2", 14)));
                }
            }
        }
        return map;
    }

    public static void main(String[] args) throws IOException {
        String templateUrl = "D:\\word\\poi-tl\\template.docx";
        String outUrl = "D:\\word\\poi-tl\\out_template.docx";

        CulturalActivity culturalActivity = new CulturalActivity();
        culturalActivity.setActivityAddr("苏州市");
        culturalActivity.setActivityName("案发带点");
        culturalActivity.setActivityType("图书馆");
        culturalActivity.setActivityRecord("飞极速地方his一定会UDAHFIUASDIY");
        culturalActivity.setHoldTime(LocalDate.now());
        culturalActivity.setPeopleNumber(100);
        Map<String, Object> map = getData(culturalActivity);

        generateWord(templateUrl, outUrl, map);

    }
}
